//
//  BIDCollectionCellView.m
//  党务通
//
//  Created by mal on 13-11-15.
//  Copyright (c) 2013年 mal. All rights reserved.
//

#import "BIDCollectionCellView.h"

const int minWidth = 18;

@implementation BIDCollectionCellView
@synthesize imgView;
@synthesize label;
@synthesize numberLabel;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        numberLabel.hidden = YES;
        NSArray *arr = [[NSBundle mainBundle] loadNibNamed:@"CollectionCellView" owner:self options:nil];
        if(arr.count<1) return nil;
        if(![[arr objectAtIndex:0] isKindOfClass:[BIDCollectionCellView class]])
        {
            return nil;
        }
        self = [arr objectAtIndex:0];
        numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 10, 30, 20)];
        [numberLabel setTextColor:[UIColor whiteColor]];
        [numberLabel setBackgroundColor:[UIColor redColor]];
        [numberLabel setFont:[UIFont systemFontOfSize:14.0f]];
        [numberLabel setTextAlignment:NSTextAlignmentCenter];
        numberLabel.layer.masksToBounds = YES;
        [self addSubview:numberLabel];
        numberLabel.hidden = YES;
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)adjustNumberLabel:(NSString *)text
{
    if(text.length>0)
    {
        if([text isEqualToString:@"0"])
        {
            numberLabel.hidden = YES;
        }
        else
        {
            numberLabel.hidden = NO;
        }
    }
    else
    {
        numberLabel.hidden = YES;
        return;
    }
    [self bringSubviewToFront:numberLabel];
    [numberLabel setText:text];
    
    CGSize size = [text sizeWithFont:numberLabel.font constrainedToSize:CGSizeMake(MAXFLOAT, 20) lineBreakMode:NSLineBreakByWordWrapping];
    CGRect labelFrame = numberLabel.frame;
    CGFloat width = size.width>minWidth?size.width:minWidth;
    CGFloat height = minWidth;
    numberLabel.layer.cornerRadius = width>minWidth?7:width/2;
    labelFrame.origin.x = self.frame.size.width - width-3;
    labelFrame.origin.y = -size.height/2+15;
    labelFrame.size.width = width;
    labelFrame.size.height = height;
    numberLabel.frame = labelFrame;
}

@end
